<template>
  <div id="useradd">
    <el-form
      :model="ruleForm"
      status-icon
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" class="inputWidth" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="昵称" class="inputWidth" prop="nickname">
        <el-input v-model="ruleForm.nickname"></el-input>
      </el-form-item>
      <el-form-item label="密码" class="inputWidth" prop="password">
        <el-input type="password" v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" class="inputWidth" prop="rePassword">
        <el-input type="password" v-model="ruleForm.rePassword"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          @click="submitForm('ruleForm')"
          type="primary"
          round
          class="btn"
          >确认添加</el-button
        >
        <el-button type="primary" round class="btn" @click="fn">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { reqSaveUser } from "../../api/users";

export default {
  name: "userAdd",
  data() {
    let nicknameValidator = (rule, value, callback) => {
      // console.log(rule, value);
      if (value == this.ruleForm.password) {
        callback();
        return;
      }
      callback("密码不一致");
    };
    return {
      ruleForm: {
        username: "",
        nickname: "",
        password: "",
        rePassword: "",
      },

      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          // {
          //   min: 6,
          //   max: 18,
          //   message: "长度在 6 到 18 个字符",
          //   trigger: "blur",
          // },
          {
            pattern: /^[0-9A-Za-z]{6,18}$/,
            message: "长度为6-18位,不能包含非法字符",
            trigger: "blur",
          },
        ],
        nickname: [
          { required: true, message: "请输入昵称", trigger: "blur" },
          {
            pattern: /^[0-9A-Za-z]{1,8}$/,
            message: "长度为1-8位的字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^[0-9A-Za-z]{6,18}$/,
            message: "长度为6-18位,不能包含非法字符",
            trigger: "blur",
          },
        ],
        rePassword: [
          { required: true, message: "请确认密码", trigger: "blur" },
          {
            validator: nicknameValidator,
            trigger: "blur",
          },
          {},
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          let res = await reqSaveUser({
            username: this.ruleForm.username,
            nickname: this.ruleForm.nickname,
            password: this.ruleForm.password,
          });
          if (res.data.code === 20002) {
            this.$message({
              message: "用户名已存在",
              type: "warning",
            });
          }
          if (res.data.code === 20001) {
            this.$message({
              message: "服务器故障",
              type: "error",
            });
          }
          if (res.data.code === 20000) {
            this.$message({
              message: "添加成功",
              type: "success",
            });
            this.$router.history.push("/users");
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    fn() {
      this.$router.history.push("/users");
    },
  },
};
</script>

<style scope>
.inputWidth {
  width: 500px;
  margin: auto;
}
#useradd {
  text-align: center;
}
.btn {
  margin-top: 30px !important;
}
</style>